iT邦幫忙

2021 iThome 鐵人賽

DAY 16
0
Mobile Development

Android Studio 30天自我挑戰系列 第 16

[Android Studio 30天自我挑戰] RadioGroup,RadioButton元件介紹

  • 分享至 

  • xImage
  •  

RaidioButton為單選元件,然而RadioGroup則是放RadioButton的選項
如果RadioButton沒有被放在RadioGroup裡則會變成複選,因此在放入RadioButton前要先新增RadioGroup。

接著用簡單的範例介紹RadioButton的用法:
先選取當前的溫度單位,透過editText輸入數值後換算
xml檔如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="85dp"
        android:layout_height="33dp"
        android:layout_marginTop="52dp"
        android:text="@string/input"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.205"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <EditText
        android:id="@+id/value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number"
        android:hint="@string/number"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.432"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.185" />
    <TextView
        android:id="@+id/degC"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/charC"
        android:textSize="50sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.761"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <TextView
        android:id="@+id/degF"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/charF"
        android:textSize="50sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.755"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.34" />
    <RadioGroup
        android:id="@+id/unit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/textView"
        app:layout_constraintTop_toTopOf="parent">
        <RadioButton
            android:id="@+id/unitF"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:checked="true"
            android:gravity="start"
            android:text="華氏"
            android:textSize="20sp" />
        <RadioButton
            android:id="@+id/uniC"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="end"
            android:text="攝氏"
            android:textAlignment="textEnd"
            android:textSize="20sp" />

    </RadioGroup>

</androidx.constraintlayout.widget.ConstraintLayout>

因為攝氏及華氏的符號所以我們在res/values/String.xml檔下新增

<string name="app_name">溫度計算</string>
<string name="input">輸入溫度</string>
<string name="number">請輸入溫度</string>
<string name="charF">"\u2109"</string>
<string name="charC">"\u2103"</string>

這樣就可以在畫面中顯示出度C及度F的符號了
接著打開MainActivity.java
先宣告需要用到的RadioGroup以及editText的值
透過calc來換算溫度的值,接著利用afterTextChanged來直接顯示於TextView上

package com.example.itradiobtn;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, TextWatcher {

    RadioGroup unit;
    EditText value;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        unit = findViewById(R.id.unit);
        unit.setOnCheckedChangeListener(this);

        value = findViewById(R.id.value);
        value.addTextChangedListener(this);
    }
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }
    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }
    @Override
    public void afterTextChanged(Editable editable) {
        calc();
    }
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        calc();
    }

    private void calc() {
        TextView degF = findViewById(R.id.degF);
        TextView degC = findViewById(R.id.degC);

        double f, c;
        if (unit.getCheckedRadioButtonId() == R.id.unitF) {
            if (value.getText().toString().length() ==0)
                return;
            else {
                f = Double.parseDouble(value.getText().toString());
                c = (f - 32) * 5 / 9;
            }
        } else {
            if (value.getText().toString().length() ==0)
                return;
            else {
                c = Double.parseDouble(value.getText().toString());
                f = c * 9 / 5 + 32;}
        }
        degC.setText(String.format("%.1f", c) + getResources().getString(R.string.charC));
        degF.setText(String.format("%.1f", f) + getResources().getString(R.string.charF));

    }
}

顯示結果為:
https://ithelp.ithome.com.tw/upload/images/20210930/20139258LYMuEPc6bh.png
https://ithelp.ithome.com.tw/upload/images/20210930/20139258Kt9xrDrT6y.png
https://ithelp.ithome.com.tw/upload/images/20210930/20139258Yv0SDMz5GA.png


上一篇
[Android Studio 30天自我挑戰] Switch case語法練習
下一篇
[Android Studio 30天自我挑戰] Radiobutton和Checkbox的練習
系列文
Android Studio 30天自我挑戰30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言